package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.aspect.annotation.SaveCommonFieldAnno;
import com.sky.aspect.annotation.UpdateCommonFieldAnno;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DishMapper {

    /**
     * 根据分类id查询菜品数量
     * @param categoryId
     * @return
     */
    @Select("select count(id) from dish where category_id = #{categoryId}")
    Integer countByCategoryId(Long categoryId);

    /**
     * 根据菜品名从菜品表查询菜品
     * @param name
     * @return
     */
    @Select("select id, name, category_id, price, image, description, status, create_time, update_time, create_user, update_user from dish " +
            "where name = #{name}")
    Dish getByName(String name);

    /**
     * 根据菜品名从菜品表查询菜品
     * @param name
     * @return
     */
    @Select("select id, name, category_id, price, image, description, status, create_time, update_time, create_user, update_user from dish " +
            "where name like concat('%', #{name}, '%')")
    List<Dish> getListByName(String name);

    /**
     * 插入菜品信息到菜品表
     * @param dish
     */
    @SaveCommonFieldAnno
    @Insert("insert dish(name, category_id, price, image, description, status, create_time, update_time, create_user, update_user) " +
            "values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void save(Dish dish);

    /**
     * 菜单分页查询
     * @param dto
     * @return
     */
    Page<Dish> page(DishPageQueryDTO dto);

    /**
     * 根据菜品id从菜品表查询菜品
     * @param dishId
     * @return
     */
    @Select("select id, name, category_id, price, image, description, status, create_time, update_time, create_user, update_user from dish " +
            "where id = #{id}")
    Dish getById(long dishId);

    /**
     * 根据id从菜品表删除菜品信息
     * @param dishId
     */
    @Delete("delete from dish where id = #{dishId}")
    void del(long dishId);

    /**
     * 根据菜品id从菜品表修改菜品信息
     * @param dish
     */
    @UpdateCommonFieldAnno
    void updateById(Dish dish);

    /**
     * 根据分类id从菜品表查询菜品信息
     * @param categoryId
     * @return
     */
    @Select("select id, name, category_id, price, image, description, status, create_time, update_time, create_user, update_user from dish " +
            "where category_id = #{categoryId} and status = 1")
    List<Dish> getByCategoryId(Long categoryId);

    /**
     * 根据分类id从菜品表查询菜品信息
     * @param dish
     * @return
     */
    @Select("select id, name, category_id, price, image, description, status, create_time, update_time, create_user, update_user from dish " +
            "where category_id = #{categoryId} and status = 1")
    List<Dish> getByDishCategoryId(Dish dish);
}